<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
        $this->menu();    //将导航拿出来
        $this->top();

        $this->display('layout');
    }
    public function chart(){
        $admin_id = session('user_id');
        $role = M('AdminRole')->where("admin_id=$admin_id")->find();
        $role_id = $role['role_id'];
        $RoleData = M('Role')->find($role_id);
        $id = $RoleData['jibie'];
        $data = M('Qujieshequ')->find($id);
        $this->assign('data',$data);


        // $res = M('Admin')->find(session('user_id'));
        // $result = M('setindex')->select($res['shouye']);
        // foreach ($result as $v){
        //     $bing[] = $this->fetch($v['url']);
        // }
        // $this->assign('bing',$bing);
        $this->display('qu_ren');
    }
    public function top(){
        $realName = session('adminUser')['realname'];
        $this->assign('realname',$realName);
    }
    public function menu()
    {
        $user_id = session('user_id');
        /********** 取出当前管理员所拥有的前两级的权限 ************/
        // 取出当前管理员所有的权限
        if($user_id == 1)
            $sql = 'SELECT * FROM zn_privilege';
        else
            $sql = 'SELECT b.*
			  FROM zn_role_privilege a
			   LEFT JOIN zn_privilege b ON a.pri_id=b.id
			   LEFT JOIN zn_admin_role c ON a.role_id=c.role_id
			    WHERE c.admin_id='.$user_id;
        $db = M();
        $pri = $db->query($sql);
        $btn = array();  // 放前两级的权限
        // 从所有的权限中取出前两级的权限
        foreach ($pri as $k => $v)
        {
            // 找顶级权限
            if($v['parent_id'] == 0)
            {
                // 再循环把这个顶级权限的子权限
                foreach ($pri as $k1 => $v1)
                {
                    if($v1['parent_id'] == $v['id'])
                    {
                        $v['children'][] = $v1;
                    }
                }
                $btn[] = $v;
            }
        }
        $this->assign('btn', $btn);
    }

}